<?php

namespace api\controllers;

use api\models\ActiveGoodForm;
use yii\web\Response;

class ActiveGoodController extends \yii\web\Controller
{

    /**
     * 数据库查询 json返回
     * http://api.spike.com/active-good/json-list
     */
    public function actionJsonList()
    {
        $act_good_form = new ActiveGoodForm();
        $act_good_list = $act_good_form->getValidActiveGoodList();

        \Yii::$app->response->format= Response::FORMAT_JSON;
        \Yii::$app->response->data = $act_good_list;
    }

    /**
     * redis缓存
     * http://api.spike.com/active-good/redis-cache
     */
    public function actionRedisCache()
    {
        $act_good_list = \Yii::$app->redis->get( 'act_good_list' );

        if( !$act_good_list ){
            $act_good_form = new ActiveGoodForm();

            $act_good_list_arr = $act_good_form->getValidActiveGoodList();
            $act_good_list = json_encode( $act_good_list_arr );

            \Yii::$app->redis->set( 'act_good_list',json_encode( $act_good_list ) );
        }

        die( $act_good_list );
    }

    /**
     * 文件缓存
     * http://api.spike.com/active-good/file-cache.html
     */
    public function actionFileCache()
    {
        // 获取 params.php 配置文件参数
        $file_path = \Yii::$app->params[ 'root_path' ] . 'active-good/file_cache.'
            . \Yii::$app->params[ 'staticTempleFlag' ];

        //根据有效期来生成页面缓冲文件 filemtime获取到文件最后修改时间
        if( is_file( $file_path ) && ( time()-filemtime( $file_path ) )<300){
            return $this->renderFile( $file_path );
        }else{

            $act_good_form = new ActiveGoodForm();
            $act_good_list_arr = $act_good_form->getValidActiveGoodList();

            $file_content = $this->render( 'file-cache', [ 'good_list' => $act_good_list_arr,'cache_id' => 'file-cache' ] );

            //ob_get_contents()从文件缓冲区中获取内容 ob_start(); ob_get_clean获取到内容并且清空缓冲区
            $ret = file_put_contents( $file_path,$file_content );
            return $this->renderFile( $file_path );
        }
        // return $this->render( 'file_cache', [ 'good_list' => $act_good_list_arr,'cache_id' => 'file_cache' ] );
    }

    /**
     * http://api.spike.com/?r=active-good/active-good-list
     * 活动商品列表页
     * @return string
     */
    public function actionActiveGoodList()
    {
        // 获取 params.php 配置文件参数
        $dir_path = \Yii::$app->params[ 'root_path' ] . 'active-good';
        $file_path = $dir_path.'/active-good-list.' . \Yii::$app->params[ 'staticTempleFlag' ];

        // 有效 缓存文件存在, 直接返回
        if( is_file( $file_path ) && ( time() - filemtime( $file_path ) )<300){
            return $this->renderFile( $file_path );
        }

        // 活动商品数据查询
        $act_good_form = new ActiveGoodForm();
        $act_good_list_arr = $act_good_form->getValidActiveGoodList();

        // 缓存文件过期 / 不存在, 重新创建
        if ( file_exists( $file_path ) ) { unlink( $file_path ); }

        // 检查目录是否存在 / 需要创建
        $ret2mkdir = $act_good_form->sp_mkdirs( $dir_path );

        // 目录创建不成功, 记录错误信息, 交由Yii2渲染页面
        if( !$ret2mkdir[ 'status' ] ){
            return $this->render( 'active-good-list', [ 'good_list' => $act_good_list_arr,'cache_id' => 'file_cache' ] );
        }

        // 缓存文件
        $file_content = $this->render( 'active-good-list', [ 'good_list' => $act_good_list_arr,'cache_id' => 'file_cache' ] );

        //ob_get_contents()从文件缓冲区中获取内容 ob_start(); ob_get_clean获取到内容并且清空缓冲区
        $ret = file_put_contents( $file_path,$file_content );
        return $this->renderFile( $file_path );

    }
}
